﻿@model Model.ETL_TaskInfo
@{
    ViewBag.Title = "编辑任务";
    Layout = "~/Views/Shared/_Main.cshtml";
}
<script type="text/javascript" src="/Res/MyJs.js"></script>
<script type="text/javascript">
    function Save() {
        if ($('#ff').form("validate")) {
            var ftype = 0;
            $('.rad_FType').each(function () {
                if ($(this)[0].checked == true)
                    ftype = $(this).val();
            });
            var FHM;
            var THM;
            if (ftype == "0") {
                $('#txt_TaskStartTime').val($('.OnceH').val() + ":" + $('.OnceM').val() + ":00");
            }
            else {
                var dtm = parseInt($('.IntervalH').val() * 60) + parseInt($('.IntervalM').val());
                if (dtm < 5) {
                    alert("任务执行间隔时间不能为0！");
                    return false;
                }
                $('#txt_TaskEveryDayIntervalMinute').val(dtm);
                $('#txt_TaskStartTime').val($('.IntervalFH').val() + ":" + $('.IntervalFM').val() + ":00");
                FHM = parseInt($('.IntervalFH').val() * 60) + parseInt($('.IntervalFM').val());
                THM = parseInt($('.IntervalTH').val() * 60) + parseInt($('.IntervalTM').val());
                if (FHM >= THM) {
                    alert("任务执行间隔结束时间必须大于开始时间！");
                    return false;
                }
            }
            $('#txt_FrequencyType').val(ftype);
            $('#txt_TaskEndTime').val($('.IntervalTH').val() + ":" + $('.IntervalTM').val() + ":00");


            if ($.trim($('#txt_TaskName').val()) == "") {
                alert("任务名称不能为空！");
                return false;
            }
            var result = $("#txt_TaskName").val().match(/^\w+$/);
            if (result == null) {
                alert("任务名称格式不正确，只能由数字、英文字母或者下划线组成!");
                return false;
            }
            if (checkNum($('#txt_TaskIntervalDays').val()) == "") {
                alert("任务执行周期应大于1天！");
                return false;
            }
            $('#txt_TaskStartDate').val($(".txt_TaskStartDate").datebox('getValue'));
            $('#txt_TaskEndDate').val($(".txt_TaskEndDate").datebox('getValue'));
            //
            var arr_sdate = $('#txt_TaskStartDate').val().split("-");
            var starttime = new Date(arr_sdate[0], arr_sdate[1], arr_sdate[2]);
            var starttimes = starttime.getTime();

            var arr_edate = $('#txt_TaskEndDate').val().split("-");
            var lktime = new Date(arr_edate[0], arr_edate[1], arr_edate[2]);
            var lktimes = lktime.getTime();

            if (arr_sdate.length == 0 || arr_edate.length == 0) {
                alert("日期格式错误。");
                return false;
            } 
            if (starttimes > lktimes) {
                alert("失效日期必须大于生效日期。");
                return false;
            }
            //            //
            //            var dataNow = new Date();
            //            if (edate < dataNow) {
            //                alert("失效日期必须大于当前日期。");
            //                return false;
            //            }
            var stepIDs = "";
            var order = 1;
            $("#right div").each(function (i, d) {
                var stepArr = new Array();
                stepArr.push($(this).attr("class"));
                stepArr.push($(this).attr("name"));
                stepArr.push(order);
                order += 1;
                stepIDs += eval(stepArr) + "&";
            });
            $('#txt_JobStepID').val(stepIDs);
            if (stepIDs.length < 1) {
                alert("请选择可选任务明细！");
                return false;
            }

            $.post("/SysJob/SaveItem?dbName=" + encodeURI($.getUrlParam("dbName")), { query: jv.GetDivJson($("#formArea"), 4) }, function (res) {
                if (res == "1") {
                    alert("操作成功！");
                    back();
                } else if (res == "2") {
                    alert("存在相同的任务名称！");
                }
                else
                    alert(res);
            });
        }
    }
    function back() {
        window.location.href = "/SysJob/JobList?dbName=" + encodeURI($.getUrlParam("dbName"));
    }
    function SelectDate(id) {
        $('#' + id).datebox('enable');
    }
    function CKleft(id, t) {
        if (id == "T") {
            $("#right").append("<div class=\"ALL\" name='T' onclick=\"CKright('T');\">所有加工数据</div>");
            $("#left .ALL[name='" + id + "']").hide();
        } else if (id == "L") {
            $("#right").append("<div class=\"ALL\" name='L' onclick=\"CKright('L');\">所有视图组</div>");
            $("#left .ALL[name='" + id + "']").hide();
        } else {
            if ($("#right .ALL[name='" + t + "']").length == 0) {
                $("#right").append("<div class='" + id + "' name='" + t + "' onclick=\"CKright('" + id + "');\">" + $("#left ." + id).html() + "</div>");
                $("#left ." + id).hide();
            }
        }
    }
    function CKright(id) {
        if (id == "T" || id == "L") {
            $("#left .ALL[name='" + id + "']").show();
            $("#right .ALL[name='" + id + "']").remove();
        }
        else {
            $("#right ." + id).remove();
            $("#left ." + id.toUpperCase()).show();
        }
    }
    function selectAll() {
        $("#left .ALL").each(function () {
            if ($(this).attr("style") != "display: none;")
                CKleft($(this).attr("name"), '');
        });
        $("#left div").each(function () {
            if (!($(this).attr("style") == "display: none;" || $(this).attr("class") == "ALL"))
                CKleft($(this).attr("class"), $(this).attr("name"));
        });
    }
    function clearAll() {
        $("#right div").each(function () {
            CKright($(this).attr("class"));
        });
    }
    $(function () {
        var tid = $('#hid_TaskID').val();
        $.post("/SysJob/GetStep/?dbName=" + encodeURI($.getUrlParam("dbName")) + "&id=" + tid, function (res) {
            $("#left").append(res);
        });
        $.post("/SysJob/GetDetail/?dbName=" + encodeURI($.getUrlParam("dbName")) + "&id=" + tid, function (res) {
            $("#right").append(res);
        });

        for (var t = 0; t <= 23; t++) {
            $('.sel_TimeH').append("<option value='" + t + "'>" + t + "</option>");
        }
        for (var t = 0; t <= 59; t = t + 5) {
            $('.sel_TimeM').append("<option value='" + t + "'>" + t + "</option>");
        }
        $(".IntervalM").val("5");
        $(".IntervalTH").val("23");
        $(".IntervalTM").val("55");
        if (tid != "") {
            var ftype = $("#txt_FrequencyType").val();
            $('.rad_FType').each(function () {
                if ($(this).val() == ftype)
                    $(this).attr("checked", "checked");
                else
                    $(this).removeAttr("checked");
            });
            var tst = $("#txt_TaskStartTime").val();
            if (ftype == 0) {
                var h = tst.split(":");
                $('.OnceH').val(h[0]);
                $('.OnceM').val(parseInt(h[1]));
            }
            else {
                var h = tst.split(":");
                $('.IntervalFH').val(h[0]);
                $('.IntervalFM').val(parseInt(h[1]));
                var tet = $("#txt_TaskEndTime").val();
                h = tet.split(":");
                $('.IntervalTH').val(h[0]);
                $('.IntervalTM').val(parseInt(h[1]));
                var im = parseInt($("#txt_TaskEveryDayIntervalMinute").val());
                var ihour = parseInt(im / 60);
                var imin = im % 60;
                $('.IntervalH').val(parseInt(ihour));
                $('.IntervalM').val(imin);
            }
        }
    }) 
</script>
<style type="text/css">
    #formArea #left, #right
    {
        width: 350px;
        height: 300px;
        overflow-y: scroll;
        background-color: #ffffff;
        border: 1px solid #cccccc;
        padding: 3px;
    }
    #left, #right div
    {
        cursor: pointer;
        white-space: nowrap;
    }
    .ALL
    {
        color: #ff0000;
    }
    .bg
    {
        background-color: #efefef;
    }
</style>
<div style="margin: 5px;">
    <div class="easyui-panel" style="width: 1020px; padding: 10px 5px 10px 5px;" data-options="title:'编辑任务',iconCls:'icon-edit'">
        <form id="ff" method="post">
        <table id="formArea" class="table_bg" width="1000px">
            <tr>
                <td>
                    任务名称
                </td>
                <td width="200px;">
                    <input type="hidden" id="hid_TaskID" value="@Model.TaskID" />
                    <input id="txt_TaskName" type="text" value="@Model.TaskName" class="easyui-validatebox" data-options="required:true"/>
                </td>
                <td>
                    任务类型
                </td>
                <td colspan="3">
                    <select id="sel_TaskType">
                        <option value="0" @(Model.TaskType == 0 ? "selected='selected'" : "")>重复执行</option>
                        <option value="1" @(Model.TaskType == 1 ? "selected='selected'" : "")>执行1次后删除</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    任务执行周期
                </td>
                <td>
                    <input id="txt_TaskIntervalDays" type="text" style="width:105px;" onkeyup="this.value=checkNum(this.value)" value="@Model.TaskIntervalDays" class="easyui-validatebox" data-options="required:true"/>天
                </td>
                <td>
                     
                </td>
                <td colspan="3">
                    <input type="hidden" id="chk_UsedMailReport" value="0"  /> 
                    <input type="hidden" id="chk_UsedSMSReport" value="0" /> 
                    <input type="hidden" id="chk_UsedRTXReport" value="0"  /> 
                </td>
            </tr>
            <tr>
                <td>
                    任务执行时间
                </td>
                <td colspan="3">
                    每天频率：<br />
                    <input type="radio" class="rad_FType" name="rad_FType" checked="checked" value="0" />执行一次：
                    时：<select class="sel_TimeH OnceH">
                    </select>：分<select class="sel_TimeM OnceM"></select>
                    <select class="sel_TimeS" style="display: none">
                        <option value="0"></option>
                    </select><br />
                    <input type="radio" class="rad_FType" name="rad_FType" value="1" />执行间隔： 时：<select
                        class="sel_TimeH IntervalH">
                    </select>：分<select class="sel_TimeM IntervalM"></select>
                    <select class="sel_TimeS" style="display: none">
                        <option value="0"></option>
                    </select>
                    从： 时:<select class="sel_TimeH IntervalFH">
                    </select>：分<select class="sel_TimeM IntervalFM"></select>
                    到： 时:<select class="sel_TimeH IntervalTH">
                    </select>：分<select class="sel_TimeM IntervalTM"></select>
                    <input id="txt_FrequencyType" type="hidden" value="@Model.FrequencyType"/>
                    <input id="txt_TaskStartTime" type="hidden" value="@Model.TaskStartTime"/>
                    <input id="txt_TaskEndTime" type="hidden" value="@Model.TaskEndTime"/>
                    <input id="txt_TaskEveryDayIntervalMinute" type="hidden" value="@Model.TaskEveryDayIntervalMinute"/>
                </td>
            </tr>
            <tr>
                <td>
                    任务生效日期
                </td>
                <td>
                    <input type="text" value="@Model.TaskStartDate" class="easyui-datebox txt_TaskStartDate" onclick="SelectDate('txt_TaskStartDate');"/>
                    <input id="txt_TaskStartDate" type="hidden" value="@Model.TaskStartDate"/>
                </td>
                <td>
                    任务失效日期
                </td>
                <td>
                    <input type="text" value="@Model.TaskEndDate" class="easyui-datebox txt_TaskEndDate" onclick="SelectDate('txt_TaskEndDate');"/>
                    <input id="txt_TaskEndDate" type="hidden" value="@Model.TaskEndDate"/>
                </td>
            </tr>
            <tr>
                <td>
                    可选任务明细
                </td>
                <td>
                    <input id="txt_TaskStatus" type="hidden" value="@Model.TaskStatus"/>
                    <input id="txt_JobStepID" type="hidden" value="@Model.JobStepID"/>
                    <div id="left">
                    </div>
                </td>
                <td style="border-left: 0px; text-align: center">
                    <input type="button" value=">>" onclick="selectAll();" /><br />
                    <br />
                    <input type="button" value="<<" onclick="clearAll();" />
                </td>
                <td colspan="3" style="border-left: 0px;">
                    <div id="right">
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    任务描述
                </td>
                <td colspan="5">
                    <textarea id="txt_TaskDescription" cols="99" rows="4">@Model.TaskDescription</textarea>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td colspan="5">
                    <input type="button" value=" 保存 " onclick="return Save();" />
                    <input type="button" value=" 返回 " onclick="back();" />
                </td>
            </tr>
        </table>
        </form>
    </div>
</div>
